//
// Created by jmdvirus on 2019/11/12 上午11:37.
// Copyright (c) 2019 jmdvirus All rights reserved.
//
//     ******          *             *--*
//       ||            *             |
//       ||  *--*--*---*  * *-********--*
//   *  //   *  *  *   *\ / ||  |  |    |
//    **    *  *  *---* *  *   *--* *--*
//

#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>

int split_files(const char *srcfile, const char *dstfile)
{
    FILE *fin  = fopen(srcfile, "r");
    FILE *fout = fopen(dstfile, "w");
    if (!fin || !fout)
    {
        printf("Open file error %d:%s\n", errno, strerror(errno));
        return -1;
    }

    size_t readline = 0;
    char * linedata = NULL;
    size_t linesize = 0;

    bool isCheckFirst = false;
    bool markNum      = false;

    char *lastline      = (char *)malloc(10240);
    lastline[0]         = '\0';
    size_t lastline_len = 0;
    while ((readline = getline(&linedata, &linesize, fin)) != -1)
    {
        if (!isCheckFirst)
        {
            if (isdigit(linedata[0]))
            {
                markNum = true;
            }
            isCheckFirst = true;
        }

        if (strlen(lastline) == 0)
        {
            strncpy(lastline, linedata, readline);
            lastline_len = strlen(lastline);
            continue;
        }

        if (markNum)
        {
            if (isdigit(linedata[0]))
            {
                fwrite(lastline, lastline_len, 1, fout);
                strncpy(lastline, linedata, readline);
                lastline[readline] = '\0';
                lastline_len       = readline;
            }
            else
            {
                if (lastline[lastline_len - 1] == '\n')
                {
                    lastline[lastline_len - 1] = '\0';
                }
                strncpy(lastline + lastline_len - 1, linedata, readline);
                lastline_len = strlen(lastline);
            }
        }
        else
        {
            if (!isdigit(linedata[0]))
            {
                fwrite(lastline, lastline_len, 1, fout);
                strncpy(lastline, linedata, readline);
                lastline[readline] = '\0';
                lastline_len       = readline;
            }
            else
            {
                if (lastline[lastline_len - 1] == '\n')
                {
                    lastline[lastline_len - 1] = '\0';
                }
                strncpy(lastline + lastline_len - 1, linedata, readline);
                lastline_len = strlen(lastline);
            }
        }
    }

    // last line
    fwrite(lastline, lastline_len, 1, fout);

    fclose(fout);
    fclose(fin);
    return 0;
}

int main(int argc, char **args)
{
    char *srcfile = NULL, *dstfile = NULL;
    int   c = -1;
    while ((c = getopt(argc, args, "s:d:h")) != -1)
    {
        switch (c)
        {
        case 's':
            srcfile = optarg;
            break;
        case 'd':
            dstfile = optarg;
            break;
        case 'h':
        default:
            printf("Usage: %s -s [srcfile] -f [dstfile]\n", args[0]);
            break;
        }
    }

    if (!srcfile || !dstfile)
    {
        printf("Usage: %s -s [srcfile] -f [dstfile]\n", args[0]);
        return -1;
    }

    printf("split_files: [%s] => [%s]\n", srcfile, dstfile);
    split_files(srcfile, dstfile);

    return 0;
}
